{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8412d65a",
   "metadata": {},
   "source": [
    "# 本周学习知识点\n",
    "> **函数的定义\n",
    "/如何使用函数\n",
    "/函数的返回值\n",
    "/使用注释改进文档\n",
    "/传递任意数量的实参\n",
    "/函数与模块**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98e6116c",
   "metadata": {},
   "source": [
    "# 1.函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f6b46c8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(x):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello \"+x+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5de8087a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello criska~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"criska\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "74427868",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"Y=2x的函数计算\"\"\"\n",
    "    print(x+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76e80c82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "963890b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def nfu_news_url(breed,page):\n",
    "    for page in range(page):\n",
    "        print(\"http://www.nfu.edu.cn/\"+breed+\"/index\"+str(page)+\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "128c58e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://www.nfu.edu.cn/tzgg/index0.htm\n",
      "http://www.nfu.edu.cn/tzgg/index1.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6e6f83eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def web_search(where,page):\n",
    "    print(\"https://www.nfu.edu.cn/\"+where+\"/index\"+str(page)+\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9b66397c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/tzgg/index2.htm\n"
     ]
    }
   ],
   "source": [
    "web_search(\"tzgg\",2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0bdf3723",
   "metadata": {},
   "outputs": [],
   "source": [
    "def web_search(where,page):\n",
    "    for a in range(page):\n",
    "        print(\"https://www.nfu.edu.cn/\"+where+\"/index\"+str(a)+\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5a1283c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/xydt/index0.htm\n",
      "https://www.nfu.edu.cn/xydt/index1.htm\n",
      "https://www.nfu.edu.cn/xydt/index2.htm\n",
      "https://www.nfu.edu.cn/xydt/index3.htm\n",
      "https://www.nfu.edu.cn/xydt/index4.htm\n",
      "https://www.nfu.edu.cn/xydt/index5.htm\n",
      "https://www.nfu.edu.cn/xydt/index6.htm\n",
      "https://www.nfu.edu.cn/xydt/index7.htm\n",
      "https://www.nfu.edu.cn/xydt/index8.htm\n",
      "https://www.nfu.edu.cn/xydt/index9.htm\n",
      "https://www.nfu.edu.cn/xydt/index10.htm\n",
      "https://www.nfu.edu.cn/xydt/index11.htm\n",
      "https://www.nfu.edu.cn/xydt/index12.htm\n",
      "https://www.nfu.edu.cn/xydt/index13.htm\n",
      "https://www.nfu.edu.cn/xydt/index14.htm\n",
      "https://www.nfu.edu.cn/xydt/index15.htm\n",
      "https://www.nfu.edu.cn/xydt/index16.htm\n"
     ]
    }
   ],
   "source": [
    "web_search(\"xydt\",17)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eb411932",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w\n",
      "w\n",
      "w\n",
      "w\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(\"w\")\n",
    "for p in range(4):\n",
    "    print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4573f564",
   "metadata": {},
   "source": [
    "> 1.positional argument(位置参数）\n",
    "> \n",
    "> 1.keyword argument（关键字参数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0c19e92a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "16f0800a",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3819912617.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [16]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)# positional argument follows keyword argument 位置参数后面要接关键字参数\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)# positional argument follows keyword argument 位置参数后面要接关键字参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "417a66ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "893da907",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "f(x=9,y=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e39036d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "f(9,y=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "413f9705",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "75e52734",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "9\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "f(0,9,4)# 位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "32dedebe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "4\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "f(x=0,z=9,y=4)#关键字参数不考虑顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9e801a6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "4\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "f(0,z=9,y=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "943eed28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "4\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "f(0,y=4,z=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "45f05e9b",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (8483260.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [25]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(0,z=9,4)\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(0,z=9,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f64c3836",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (2341521733.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [26]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=0,z=9,4)\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=0,z=9,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a958210",
   "metadata": {},
   "source": [
    "# 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f57dbf44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d1f26ca9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " I have a  cat , it's a  咪咪 .\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息，如果您的爱宠是dog，可以选择不填写animal_type\"\"\"\n",
    "    print(\"\\n I have a \",animal_type,\",\",\"it's a \",pet_name,\".\")\n",
    "    \n",
    "describe_pet(\"咪咪\",\"cat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9569d5bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " I have a  dog , it's a  嘟嘟 .\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"嘟嘟\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bf7f734c",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-default argument follows default argument (1435907919.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [30]\u001b[1;36m\u001b[0m\n\u001b[1;33m    def describe_pet(animal_type=\"dog\",pet_name):\u001b[0m\n\u001b[1;37m                                               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
     ]
    }
   ],
   "source": [
    "#未定义的参数后面跟已经定义的参数\n",
    "def describe_pet(animal_type=\"dog\",pet_name):\n",
    "    \"\"\"显示宠物信息，如果您的爱宠是dog，可以选择不填写animal_type\"\"\"\n",
    "    print(\"\\n I have a \",animal_type,\",\",\"it's a \",pet_name,\".\")\n",
    "describe_pet(\"咪咪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1518fea3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thank thank thank\n",
      "thank||||thank||||thank\n"
     ]
    }
   ],
   "source": [
    "print(\"thank\",\"thank\",\"thank\")\n",
    "print(\"thank\",\"thank\",\"thank\",sep='||||')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b4747e00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "world happy world happy world happy world happy\n",
      "world happy\n",
      "world happy\n",
      "worldhappy\n",
      "worldhappy\n",
      "worldhappy\n"
     ]
    }
   ],
   "source": [
    "for i in range(3):\n",
    "    print(\"world\",\"happy\",end=\" \")\n",
    "for i in range(3):\n",
    "        print(\"world\",\"happy\")\n",
    "for i in range(3):\n",
    "    print(\"world\",\"happy\",sep=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "41633b6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好 世界\n",
      "你好 世界\n",
      "你好世界\n"
     ]
    }
   ],
   "source": [
    "print(\"你好\",\"世界\",end=\"\\n\")\n",
    "print(\"你好\",\"世界\")\n",
    "print(\"你好\",\"世界\",end=\"\\n\",sep=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "845ff3df",
   "metadata": {},
   "source": [
    "# 函数的返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe029064",
   "metadata": {},
   "source": [
    "> 几乎所有编程语言，函数是要求必须有返回值\n",
    ">\n",
    "> 唯独Python作为高级语言，可以不写返回值,但一般情况下还是要写的，不写返回None\n",
    ">\n",
    "> 函数的返回值return :\n",
    ">>\n",
    ">> return:可以返回过程运算结果\n",
    ">>\n",
    ">> return: 可以返回过程（自定义结果）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "911a792b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1ef5489e",
   "metadata": {},
   "outputs": [],
   "source": [
    "result=f(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "73229acc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "61bc2851",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    print(2+x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b015d100",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "f(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "299c22a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y=2+x\n",
    "    return y\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cb4e7e41",
   "metadata": {},
   "outputs": [],
   "source": [
    "result=f(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "68aa766e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67be7c30",
   "metadata": {},
   "source": [
    "# 函数的优势"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4f8f58b",
   "metadata": {},
   "source": [
    "* 可重复使用\n",
    "* 具有强大的可扩展性\n",
    "* 具有可定制化"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
